<template>
  <div class="app-container">
    <el-card shadow="never" v-loading="loading">
      <el-row :gutter="5">
        <el-col :span="24" class="main-card-view">
          <el-row>
            <el-card shadow="never" class="top-search-view-card">
              <el-row>
                <el-form :inline="true" :model="searchForm" class="demo-form-inline search-view">
                  <el-form-item label="关键字">
                    <el-input v-model="searchForm.keyWord" placeholder="关键字"></el-input>
                  </el-form-item>
                  <el-form-item label="">
                    <Treeselect v-model="searchForm.classifyUid" :options="data" :normalizer="searchProps"
                      :multiple="false" placeholder="请选择" @select="handleNodeClick" clearable append-to-body
                      style="width: 240px;">
                    </Treeselect>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="handleSearch" class="search-button">查询</el-button>
                    <el-button @click="recoveryBatch" class="del-button">批量还原
                    </el-button>
                    <el-button @click="deleteBatch" class="del-button">批量删除
                    </el-button>
                  </el-form-item>
                </el-form>
              </el-row>
            </el-card>
          </el-row>

          <el-row>
            <el-card shadow="never">
              <el-table :data="tableData" :stripe="true" style="width: 100%" @selection-change="handleSelection">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="titleName" label="标题" />
                <el-table-column prop="classify" align="center" label="分类" />
                <el-table-column prop="createTime" align="center" label="创建时间" />
                <el-table-column prop="publisher" align="center" label="发布人" />
                <el-table-column prop="origin" align="center" label="来源" />
                <el-table-column align="center" label="操作" width="200">
                  <template slot-scope="scope">
                    <el-button type="text" @click="recovery(scope.row)">还原</el-button>
                    <el-button type="text" class="table-delete-btn"
                      @click="handleDelete(scope.$index, scope.row)"></el-button>
                  </template>
                </el-table-column>
              </el-table>
              <XPagination ref="pageInfo" :total="total" :get-data="getList" />
            </el-card>
          </el-row>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import XPagination from "../../components/xpagination/XPagination";
import * as recycle from "../../api/recycle"
import * as classifyManger from "../../api/classifyManger"
// import the component
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { handleTreeData } from '../../utils/tree.js'
export default {
  name: "list",
  components: { XPagination, Treeselect },
  data () {
    return {
      searchForm: {
        recycleType: '1',
      },
      tableData: [],
      treeData: [],
      loading: false,
      total: 0,
      selectedRows: [],
      searchProps (node) {
        return {
          id: node.uid,
          label: node.classifyName,
          children: node.children
        }
      },
      data: [],
      userInfo: {}
    }
  },
  methods: {
    getList (pageInfo) {
      const param = {
        ...this.searchForm,
        ...pageInfo,
        recycleOperator:this.userInfo.uid
      }
      recycle.queryByPage(param).then(response => {
        console.log(response)
        this.tableData = response.data.data.rows
        this.total = response.data.data.total
        this.loading = false
      }).catch(error => {
        console.log(error)
      })
    },
    handleDelete (index, data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = [{
          uid: data.uid,
        }]
        this.loading = true
        recycle.deleteBatch(param).then(result => {
          this.loading = false
          this.$message.success(result.data.msg)
          this.handleSearch()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    deleteBatch () {
      if (this.selectedRows.length == 0) {
        this.$message.error('请勾选数据')
        return false
      }
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let entities = []
        this.selectedRows.forEach(item => {
          entities.push({
            uid: item.uid
          })
        })
        let param = {
          userId: this.userInfo.uid,
          entities: entities
        }
        this.loading = true
        recycle.deleteBatch(entities).then(result => {
          this.loading = false
          this.$message.success(result.data.msg)
          this.handleSearch()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    handleSearch () {
      this.$refs.pageInfo.loadData()
    },
    handleNodeClick (data) {
      this.searchForm.classifyUid = data.uid;
      this.handleSearch();
    },
    recoveryBatch () {
      if (this.selectedRows.length == 0) {
        this.$message.error('请勾选数据')
        return false
      }
      let entities = []
      this.selectedRows.forEach(item => {
        entities.push({
          uid: item.uid,
          recycleType: '0'
        })
      })
      let param = {
        userId: this.userInfo.uid,
        entities: entities
      }
      recycle.batchEdit(param).then(result => {
        this.handleSearch()
      }).catch(error => {
        console.log(error)
      })
    },
    recovery (data) {
      let param = {
        userId: this.userInfo.uid,
        entities: [{
          uid: data.uid,
          recycleType: "0"
        }]
      }
      recycle.batchEdit(param).then(result => {
        this.handleSearch()
      }).catch(error => {
        console.log(error)
      })
    },
    handleSelection (selection) {
      this.selectedRows = selection;
    },
    getTree () {
      const param = {

      };
      classifyManger.queryByClassifyName(param).then(response => {
        this.data = response.data.data
        this.options = handleTreeData(response.data.data)
      }).catch(error => {
        console.log(error)
      })
    }
  },
  mounted () {
    this.userInfo = JSON.parse(localStorage.getItem('user_info'))
    this.searchForm.approvalUid = localStorage.getItem('loginUid')
    this.getTree()
    this.handleSearch()
  }
}
</script>

<style scoped lang="scss"></style>
